package com.manman.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manman.eduService.client.VodClient;
import com.manman.eduService.entity.EduVideo;
import com.manman.eduService.mapper.EduVideoMapper;
import com.manman.eduService.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-31
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    // 注入 vodClient
    @Autowired
    VodClient vodClient;

    @Override
    public void removeByCourseId(String courseId) {
        // 根据课程 id 查询所有视频 id
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        wrapperVideo.select("video_source_id");
        List<EduVideo> videoList = baseMapper.selectList(wrapperVideo);
        List<String> videoIds = new ArrayList<>();
        for (EduVideo eduVideo : videoList){
            String videoId = eduVideo.getVideoSourceId();
            if (!StringUtils.isEmpty(videoId)){
                videoIds.add(videoId);
            }
        }
        if (videoIds.size() > 0){
            vodClient.deleteBatch(videoIds);
        }

        // 删除小节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
